package tr.com.netgis;

import tr.com.netgis.util.Constants;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 
 * @author NetGis
 * 
 */
public class Preferences extends PreferenceActivity {
	
	private SetLocationApi api;
	private static final String TAG = "Preferences";
	
	OnSharedPreferenceChangeListener prefListner = new OnSharedPreferenceChangeListener(){
	      public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
              try {
            	  SharedPreferences.Editor editor = prefs.edit();
            	  editor.commit();
            	  api.sendCommand(SetLocationService.MSG_MINIMUM_DISTANCE_CHANGE_FOR_UPDATES);
				} catch (RemoteException e) {
					e.printStackTrace();
					Log.e(TAG, e.getMessage(), e);
				}				
	      }
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
        SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        Log.i(TAG, Boolean.toString(mySharedPreferences.contains("sendToServer")));
        mySharedPreferences.registerOnSharedPreferenceChangeListener(prefListner);
        /*
		// Get the custom preference
		Preference useGPSPref = (Preference) findPreference(Constants.PREFERENCES_KEY_USE_GPS);

		useGPSPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
                SharedPreferences mySharedPreferences = getSharedPreferences(Constants.PREFERENCES_KEY_USE_GPS, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = mySharedPreferences.edit();
                editor.putBoolean(Constants.PREFERENCES_KEY_USE_GPS, (Boolean) newValue);
                editor.commit();
                Toast.makeText(getApplicationContext(), newValue.toString(), 1000).show();
                //sendMessageToService(SetLocationService.MSG_MINIMUM_DISTANCE_CHANGE_FOR_UPDATES);                
                return true;
			}
		});
		*/
		bindService(Constants.SET_LOCATION_SERVICE_INTENT, serviceConnection, 0);
	}
	
	private ServiceConnection serviceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "Service connection established");
			// that's how we get the client side of the IPC connection
			api = SetLocationApi.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "Service connection closed");
		}
	};
    
	@Override     
	protected void onResume() {
	    super.onResume();          
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(prefListner);     
	}



	@Override     
	protected void onPause() {         
	    super.onPause();          
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(prefListner);
	}



	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(serviceConnection);
	}
	
	
	
}
